home *** CD-ROM | disk | FTP | other *** search
- {$A+,B-,D-,E-,F+,G-,I+,L-,N-,R-,S-,V-,X-}
- {$M 16384,0,0}
- PROGRAM HugeCalc;
- USES Calc;
- CONST
- Copyright : String[86] = 'HUGECALC 1.0, Copyright '+
- '(c) 1991 by Neil J. Rubenking'#13#10'PC Magazine '#254+
- ' Neil J. Rubenking';
- NeedComma : Boolean = FALSE;
-
- FUNCTION AllNums(VAR A : String) : Boolean;
- (* Strips commas; TRUE if string is all numeric *)
- VAR N : Byte;
- BEGIN
- AllNums := FALSE;
- FOR N := length(A) DOWNTO 1 DO
- CASE A[N] OF
- '0'..'9' : ;
- ',' : BEGIN
- Move(A[succ(N)], A[N], length(A)-N);
- Dec(A[0]);
- END;
- ELSE Exit;
- END;
- AllNums := A <> '';
- END;
-
- FUNCTION IsDevice(VAR F : Text) : Boolean; Assembler;
- ASM
- MOV AH, 44h {IOCTL function}
- MOV AL, 00h {get device info subfunction}
- LES DI, F
- MOV BX, ES:[DI] {handle in BX}
- INT 21h
- MOV AL, 0 {return value is FALSE}
- JC @end
- TEST DX, 80h {check is-device bit}
- JZ @end {if NOT set, just end}
- INC AL {return value is TRUE}
- @end:
- END;
-
- VAR op, opRand, rem : String;
- operation : Char;
-
- FUNCTION GotParams : Boolean;
- { PURPOSE : Returns true if parameters are
- correctly passed on the command line -- and
- assigns them to the correct variables if so.}
- VAR S : String[1];
- B : Byte;
-
- PROCEDURE ErrorOut(S : String);
- BEGIN
- IF IsDevice(Output) THEN
- BEGIN
- WriteLn(Copyright); WriteLn;
- WriteLn(S);
- END
- ELSE WriteLn('*ERROR*');
- Halt(1);
- END;
-
- BEGIN
- GotParams := FALSE;
- B := 2;
- IF (NOT IsDevice(Input)) AND NOT EoF(Input) THEN
- BEGIN
- ReadLn(Op);
- Dec(B);
- END;
- IF (ParamStr(ParamCount) = '/c') OR
- (ParamStr(ParamCount) = '/C') THEN
- NeedComma := TRUE
- ELSE NeedComma := IsDevice(Output);
- IF ParamCount < B THEN
- ErrorOut('Enter "HC ## op ##", where op is +,-,*,/, '+
- 'or ^'#13#10' or "HC ## !" for factorial');
- IF B = 2 THEN op := ParamStr(1);
- IF NOT AllNums(op) THEN
- ErrorOut('Not a positive integer: "'+op+'"');
- S := ParamStr(B);
- operation := S[1];
- CASE operation OF
- '!' : ;
- '+', '-', '*', '/', '^' : BEGIN
- IF ParamCount < succ(B) THEN
- ErrorOut('The operator '+operation+
- ' requires a second operand.');
- Oprand := ParamStr(B+1);
- IF NOT AllNums(OpRand) THEN
- ErrorOut('Not a positive integer: "'+OpRand+'"');
- END;
- ELSE ErrorOut('Valid operations are +,-,*,/, ! and ^');
- END;
- GotParams := TRUE;
- END;
-
- FUNCTION AddComma(WW : String) : String;
- VAR posn, MinLoc : Word;
- BEGIN
- posn := succ(length(WW));
- MinLoc := 4;
- IF WW[1] = '-' THEN Inc(MinLoc);
- WHILE (posn > MinLoc) AND (length(WW) < 255) DO
- BEGIN
- Dec(posn, 3);
- Move(WW[posn],
- WW[succ(posn)],
- succ(length(WW)-posn));
- WW[posn] := ',';
- Inc(WW[0]);
- END;
- AddComma := WW;
- END;
-
- PROCEDURE ResultOut(result : String);
- BEGIN
- IF result = '' THEN
- BEGIN
- WriteLn('*OVERFLOW*');
- Halt(2);
- END;
- IF NeedComma THEN WriteLn(AddComma(result))
- ELSE WriteLn(Result);
- END;
-
- BEGIN
- IF GotParams THEN
- BEGIN
- CASE operation OF
- '+' : BEGIN
- IF IsDevice(Output) THEN
- Write(' SUM: ');
- ResultOut(add(op, opRand));
- END;
- '-' : BEGIN
- IF IsDevice(Output) THEN
- Write('DIFFERENCE: ');
- ResultOut(sub(op, opRand));
- END;
- '*' : BEGIN
- IF IsDevice(Output) THEN
- Write(' PRODUCT: ');
- ResultOut(prod(op, opRand));
- END;
- '/' : BEGIN
- IF IsDevice(Output) THEN
- Write(' QUOTIENT: ');
- ResultOut(divide(op, opRand, rem));
- IF IsDevice(Output) THEN
- BEGIN
- Write('REMAINDER: ');
- WriteLn(AddComma(rem));
- END;
- END;
- '!' : BEGIN
- IF IsDevice(Output) THEN
- Write(' FACTORIAL: ');
- ResultOut(fact(op));
- END;
- '^' : BEGIN
- IF IsDevice(Output) THEN
- Write(' POWER: ');
- ResultOut(power(op, oprand));
- END;
- END;
- END;
- END.
-